package other;

import java.util.Arrays;

/**
 * 一个数组 从左到右依次是 从小到大然后从从大到小 写代码查找最大数字和个数
 */
public class FindBigNum {
    public static void main(String[] args) {
        System.out.println(Arrays.toString(getBigNum2(new int[]{1, 1, 3, 3, 5, 6, 7, 7, 7, 8, 10, 15, 20, 22, 23, 23, 45, 56, 80, 80, 45, 45, 43, 42, 32, 32, 14, 11, 6, 3, 3})));
        System.out.println(Arrays.toString(getBigNum2(new int[]{1, 2, 2, 3, 3, 4, 5, 5, 9, 10, 23, 23, 23, 23, 23, 78, 78, 78, 2, 2, 2, 2, 2, 1, 1, 1, 1})));
        System.out.println(Arrays.toString(getBigNum2(new int[]{8, 8, 8, 8, 8, 8, 8})));
        System.out.println(Arrays.toString(getBigNum2(new int[]{1, 3, 3, 3, 4, 5, 5, 5, 5, 5, 6, 7, 8, 8, 8, 8, 8})));
        System.out.println(Arrays.toString(getBigNum2(new int[]{9, 9, 9, 9, 9, 6, 6, 6, 2, 2, 2, 1})));
    }

    /**
     * return int[]{bigNum, num}
     */
    public static int[] getBigNum(int[] nums) {

        int[] nums2 = nums;
        int maxValue = 0;
        for (int num : nums) {
            maxValue = Math.max(maxValue, num);
        }

        int left = 0;
        int right = nums.length - 1;

        int bigNum = -1;
        int bigNumIndex = -1;

        while (left <= right) {
            int mid = left + (right - left) / 2;
            int midNum = nums2[mid];
            int leftNum = mid - 1 >= 0 ? nums2[mid - 1] : -1;
            int rightNum = mid + 1 < nums2.length ? nums2[mid + 1] : Integer.MAX_VALUE;

            if (leftNum <= midNum && rightNum < midNum) {
                bigNum = midNum;
                bigNumIndex = mid;
                break;
            }
            if (leftNum < maxValue) {
                right = mid;
            }
            if (leftNum <= midNum) {
                left = mid + 1;
            } else if (rightNum < midNum) {
                right = mid;
            }
        }

        if (bigNum == -1) {
            return new int[]{-1, 0};
        }

        int num = 0;
        for (int i = bigNumIndex; nums2[i] == bigNum; i--) {
            num++;
        }

        return new int[]{bigNum, num};
    }

    public static int[] getBigNum2(int[] nums) {
        int bigNum = -1;
        int bigNumIndex = -1;
        boolean hasLess = false;
        for (int i = 1; i < nums.length; i++) {
            int pre = i - 1;
            if (nums[pre] < nums[i]) {
                hasLess = true;
            }
            if (hasLess && nums[pre] > nums[i]) {
                bigNum = nums[pre];
                bigNumIndex = pre;
                break;
            }
        }

        if (bigNum == -1) {
            return new int[]{-1, 0};
        }
        int num = 0;
        for (int i = bigNumIndex; nums[i] == bigNum; i--) {
            num++;
        }
        return new int[]{bigNum, num};
    }


}
